package com.haixue.android.academy.view.tiku;

import android.academy.haixue.com.haixue.R;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;


public class TiikuDataView extends TextView {


	public static enum IMAGE_SOURCE {
		SDCARD, ASSERT, HTTP, RES
	}

	protected static final String TAG = "TiikuDataView";;

	private IMAGE_SOURCE source = IMAGE_SOURCE.ASSERT;
	private int maxImageWidth = -1;
   
	private String content = null;
    private boolean img=false;
    private Spanned spanned;
	public TiikuDataView(Context context) {
		super(context);
		
		// TODO Auto-generated constructor stub
	}

	public TiikuDataView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public TiikuDataView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public void setImageSource(IMAGE_SOURCE imageSource) {
		this.source = imageSource;
	}
     public void setImageClickable(boolean clickable){
    	 this.img=clickable;
     }
	public void setMaxImageWidth(int maxImageWidth) {

		this.maxImageWidth = maxImageWidth;

	}

	public void reloadData() {
		if (content == null || content.equals("")) {
			return;
		}

		setText(content);
	}

	public void setText(String string) {

		content = string;

		String singleChoiceImage = getContext().getString(R.string.string_tiiku_category_single);
		String multipleChoiceImage = getContext().getString(R.string.string_tiiku_category_multiple);
		String judgeChoiceImage = getContext().getString(R.string.string_tiiku_category_judge);
		String comprehensiveChoiceImage = getContext().getString(R.string.string_tiiku_category_comprehensive);

		String singleTitle, multipleTitle, judgeTitle, comprehensiveTitle;

		singleTitle = getResources().getString(R.string.string_tiiku_category_single_title);
		multipleTitle = getResources().getString(R.string.string_tiiku_category_multiple_title);
		judgeTitle = getResources().getString(R.string.string_tiiku_category_judge_title);
		comprehensiveTitle = getResources().getString(R.string.string_tiiku_category_comprehensive_title);

		if (string.startsWith(singleTitle)) {
			string = string.replaceFirst(singleTitle, singleChoiceImage);
		}

		if (string.startsWith(multipleTitle)) {
			string = string.replaceFirst(multipleTitle, multipleChoiceImage);
		}

		if (string.startsWith(judgeTitle)) {
			string = string.replaceFirst(judgeTitle, judgeChoiceImage);
		}

		if (string.startsWith(comprehensiveTitle)) {
			string = string.replaceFirst(comprehensiveTitle, comprehensiveChoiceImage);
		}

		string = string.replaceAll("png”", "png\"");
		string = string.replaceAll("src=“", "src=\"");
		string = string.replaceAll("src=i", "src=\"i");
		string = string.replace("．png", ".png");
		string = string.replace(".png/", ".png\"/");

		System.out.print(string);

		BaseImageGetter imageGetter = null;

		switch (source) {

		case SDCARD:
			imageGetter = new ImageGetterFromSDCard(getContext());
			break;

		case ASSERT:
			string = string.replace("src=\"image", "src=\"images/image");
			imageGetter = new ImageGetterFromAssets(getContext());
			break;

		case HTTP:
		
			
		
				
				imageGetter = new ImageGetterFromHTTP(getContext(), this);
			
			
			
		 
			
			break;

		case RES:

			imageGetter = new ImageGetterFromRes(getContext(), this);

			break;

		default:
			break;
		}

		imageGetter.setMaxImageWidth(maxImageWidth);

		String html = string;

	/*	setClickable(true);
		setMovementMethod(LinkMovementMethod.getInstance());*/
		//Spanned spanned = Html.fromHtml(html, imageGetter,new MyTagHandler(getContext()) );
		if(img){
			 spanned = Html.fromHtml(html, imageGetter,new MyTagHandler(getContext()) );
		}else{
			 spanned = Html.fromHtml(html, imageGetter,null);
		}
	
		setText(spanned);
		

		

	}
}
